<?php
/**
 * RssCollect.php
 *
 * 采集器文件，定义采集器
 *
 * PHP Version 5.6
 *
 * @category PHP
 * @package  Collector
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 * @date     2018-05-13 22:56:47
 * @modifyby ctwj
 */

namespace Telegram\Collector;

use Telegram\Collector\CollectInterface;
use Telegram\Object\Message;

/**
 * Wechat Collect
 *
 * @category PHP
 * @package  Collector
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 */
class RssCollector extends BaseCollect implements CollectInterface
{


    /**
     * 采集数据
     *
     * @return array
     */
    public function collect()
    {
        $feed = new \SimplePie();
        $feed->set_feed_url($this->resource->url);
        $feed->set_useragent(getUserAgent());
        $feed->set_cache_location(CACHE_PATH);
        $feed->init();

        if (!empty($feed->error)) {
            getService('logger')->warning($feed->error, ['resource' => $this->resource->name]);
            return [];
        }
        
        $list = $this->parseFeed($feed);
        $messages = $this->_makeMesssage($list);
        return $messages;
    }

    /**
     * Parse Feed Data
     *
     * @param [type] $feed Simplepie对象
     *
     * @return Message
     */
    public function parseFeed($feed)
    {
        $result = [];
        foreach ($feed->get_items() as $item) {
            $author = $item->get_author();
            $category = $item->get_category();
            $pic = $this->parseFirstPic($item->get_content(), $item->get_permalink());
            $data = [
                'hashkey'   => md5($item->get_id()),
                'link'      => $item->get_permalink(),
                'title'     => $item->get_title(),
                'abstract'  => \strip_tags($item->get_description()),
                'pub_date'  => $item->get_date('Y-m-d H:i:s'),
                'author'    => is_null($author) ? '' : $author->get_name(),
                'category'  => is_null($category) ? '' : $category->get_term(),
                'content'   => trim($item->get_content()),
                'pic'       => $pic
            ];
            $result[] = $data;
        }
        return $this->dataFix($result);
    }

    
    /**
     * 将数据组装为Message
     *
     * @param [type] $datas 需要装的数据
     *
     * @return array of Telegram\Object\Message
     */
    private function _makeMesssage($datas)
    {
        $messages = [];
        foreach ($datas as $data) {
            $tmp_data = [
                'hashkey'       => $data['hashkey'],
                'author'        => $data['author'],
                'category'      => $data['category'],
                'abstract'      => $data['abstract'],
                'message_url'   => $data['link'],
                'title'         => $data['title'],
                'content'       => $data['content'],
                'pub_date'      => $data['pub_date'],
                'pic'           => $data['pic']
            ];
            $messages[$data['hashkey']] = new \Telegram\Object\Message($tmp_data);
        }
        return $messages;
    }
}
